//============================================================================
// Name        : StructureXML.cpp
// Author      : xdu
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include "StructureXml.h"

using namespace std;

AttributXml::AttributXml(){}

AttributXml::AttributXml(string name, string value)
{
	this->name = name;
	this->value = value;
}


string AttributXml::GetName()
{
	return name;
}

string AttributXml::GetValue()
{
	return value;
}

void AttributXml::Afficher()
{
	cout << name << "=\"" << value << "\"";
}


ElementXml::ElementXml()
{
	this->name = "";
	this->level = -1;
}

ElementXml::ElementXml(const string& name)
{
	this->name = name;
	this->level = 0;
}

ElementXml::ElementXml(const string& name, const string& contents, int level)
{
	this->name = name;
	this->contents = contents;
	this->level = level;
}

ElementXml& ElementXml::AddChild(const ElementXml& element)
{
	childs.push_back(element);
	return childs.back();
}

void ElementXml::SetChildList(const list<ElementXml>& childList)
{
  childs = childList;
}

ElementXml& ElementXml::AddChild(const string& name, const string& contents)
{
	ElementXml e(name, contents, level+1);
	childs.push_back(e);
	return childs.back();
}

void ElementXml::AddAttribute(const string& name, const string& value)
{
	AttributXml a(name, value);
	attributes.push_back(a);
}

list<ElementXml> ElementXml::GetChildList()
{
	return childs;
}

list<AttributXml> ElementXml::GetAttributeList()
{
	return attributes;
}

void ElementXml::SetAttributeList(list<AttributXml> list)
{
	attributes = list;
}

ElementXml ElementXml::seekMatchAttValue(string value)
{

	list<ElementXml>::iterator it = childs.begin();
		while (it != childs.end())
		{
			if((*it).GetAttributeList().front().GetValue()==value)
			{
				return (*it);
			}
			it++;
		}

	ElementXml null;

	return null;


}
void ElementXml::AddContent(string msg)
{
	contents += msg;
}

void ElementXml::AddChildren(list<ElementXml> elements)
{

	list<ElementXml>::iterator itSec = elements.begin();

	while (itSec != elements.end())
	{
		childs.push_back((*itSec));
		itSec++;
	}
}

void ElementXml::RemoveBack()
{
	childs.pop_back();
}

void ElementXml::Afficher()
{
	
	list<AttributXml>::iterator i1 = attributes.begin();
	for (int i = 0; i<level; i++)
	{
		cout << " ";
	}
	if (name != "")
	   cout << "<" << name;
	while (i1 != attributes.end())
	{
		cout << " ";
		i1->Afficher();
		i1++;
	}
	if (name != "") {
	  cout<< ">";
	} else {
	  cout << contents;
	}
	list<ElementXml>::iterator i2 = childs.begin();
	if (i2 != childs.end())
	{
		if (i2 == childs.begin()) {
		  if (i2->GetName() != "")
		    cout << endl;
		}
		while (i2 != childs.end())
		{
			i2->Afficher();
			i2++;
		}
		for (int i = 0; i<level; i++)
		{
			cout << " ";
		}
	}

	if (name != "")
	  cout << "</" << name << ">" << endl;
};

ElementXml& StructureXml::GetRoot()
{
	return root;
}



StructureXml::StructureXml(){}

StructureXml::StructureXml(const ElementXml& element)
{
  root = element;
}

StructureXml::StructureXml(string name, string contents)
{
	ElementXml e;
	if (contents.empty())
	{
		ElementXml e(name);
		this->root = e;
	}
	else
	{
		ElementXml e(name, contents, 0);
		this->root = e;
	}
}

void StructureXml::Afficher()
{
	root.Afficher();
}


